package org.zcl.utils;

import org.apache.zookeeper.ZooKeeper;
import org.zcl.watcher.DefaultWatch;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @Package org.zcl.utils
 * @PROJECT_NAME: question
 * @DESCRIPTION:
 * @USER: zhuchenglin
 * @DATE: 2022/5/31 13:56
 */
public class ZKUtils {

    private static ZooKeeper zk;

    private static String address = "192.168.150.11:2181,192.168.150.12:2181,192.168.150.13:2181,192.168.150.14:2181/testLock";

    private static DefaultWatch watch = new DefaultWatch();

    private static CountDownLatch init = new CountDownLatch(1);

    public static ZooKeeper getZk() {
        try {
            zk = new ZooKeeper(address, 1000, watch);
            watch.setCc(init);
            // 等待连接成功
            init.await();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return zk;
    }
}
